import cfg from "../../config";
import {getView} from "../../util/api";
import {backPage, perKey} from "../../util/aid";
import {payorderFun} from '../common';
import Dialog from "../../lib/kdialog";
const dialog = new Dialog();

let _data = null;//父页面的数据
let _opts = null;//父页面的参数
let _order = null;//订单数据
let _currSaet = null;

const $ = require('../../lib/kdom');
let _view = require('raw-loader!./payorder.html');

export default class {
    path = '/train/payorder$';
    hash = '/train/payorder';
    title = '支付订单';

    constructor(opt) {
        opt = opt || {};
        this.path = opt.path || this.path;
        this.hash = opt.hash || this.hash;
        this.title = opt.title || this.title;
    }

    // 输出视图
    view(cb) {
        if (!_view) {
            // 静态资源浏览器有缓存,增加时标,强制刷新!
            getView(`${cfg.train.view.book}?t=${(+new Date())}`, '', (rs) => {
                _view = rs;
                cb(null, _view);
            });
        } else
            cb(null, _view);
    }

    // 在已经加载的视图上操作
    bind(dv, params) {


        _data = params.data;
        _opts = params.opts;
        _currSaet = params.currSaet;
        _order = params.order;

        // 加载订单
        loadOrderDelFun(_data, _currSaet, _order);

        // 支付
        payOrderBtn.onclick = () => {
            payorderFun(_order, _data, _opts, _currSaet);
        };

        // 取消
        concelOrderBtn.onclick = () => {
            concelOrderFun(_order.OrderNumber);
        };

        // 返回
        backPage('#pg-train-payorder', 'backPage', () => {
            $.router.go('/train/order', '', true);
        });
    }

    /*bind*/
}

function loadOrderDelFun(obj, currSaet, order) {
    // console.log(obj, currSaet, order);
    let temp = '';
    let _ageType = {1: '成人票', 2: '儿童票'};
    let _cardType = {1: '二代身份证', 2: '护照', 5: '港澳通行证', 7: '台湾居民来往大陆通行证'};
    for (let i = 0, o = order.Tickets; i < o.length; i++) {
        temp += `<div class="card-content-cell">
              <span>${o[i].PassengerName}</span>
              <span>${_cardType[o[i].passengerIdCardType]}</span>
              <span>${_ageType[o[i].TicketType]}</span>
            </div>`;
    }
    let rc = `<div class="content-block-title">单号：${order.OrderNumber}</div>
    <div class="list-block cards-list">
        <ul>
            <li class="card">
                <div class="card-header">
                    <span>${obj.StartCity} - ${obj.EndCity}</span>
                    <span class="text-danger">代付款</span>
                </div>
                <div class="card-content">
                    <div class="card-content-inner">
                        <div class="card-content-cell">
                            <span>${obj.TrainCode}</span>
                            <span>${currSaet.seat}</span>
                            <b class="text-danger"><span class="f06">￥</span>${order.OrderAmount}</b>
                        </div>
                       ${temp}
                    </div>
                </div>
                <div class="card-footer">
                    <span>${_opts.date} ${obj.StartTime}开</span>
                    <span>${obj.CostTime}</span>
                </div>
            </li>
        </ul>
    </div>`;
    $.html(payOrderDelBox, rc);
}

/*function payorderFun() {
 dialog.loading();
 const formData = `action=confirmpayorder&orderno=${_order.OrderNumber}&price=${_order.OrderAmount}`;
 console.log(formData);
 // 测试
 ajax.get(`${cfg.train.host}${cfg.train.getAct}`, formData, (rs) => {
 if (rs) {
 dialog.close();
 console.log(rs);
 let r = JSON.parse(rs);
 if (r.successcode === 'T') {
 dialog.close();
 $.router.go('/train/pay-result', {data: _data, opts: _opts, order: _order, result: r, currSaet: _currSaet}, true);
 } else {
 dialog.close();
 dialog.alert({content: r.Info || '非常抱歉，订单支付失败，请刷新或者退出重试！'}, () => {
 $.router.go('/train/pay-result', {data: _data, opts: _opts, order: _order, result: r, currSaet: _currSaet}, true);
 });
 }
 }
 });
 }*/

